Attribute VB_Name = "modUnFocusableHookImpl"
Public Declare Function CallWindowProc Lib "USER32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = (-4)
Public Const WM_SETFOCUS = &H7
Private mhWnd As Long
Private OldProc As Long

Function WinProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If wMsg = WM_SETFOCUS Then Exit Function
    
    WinProc = CallWindowProc(OldProc, hWnd, wMsg, wParam, lParam)
End Function

Public Sub Hook(ByVal hWnd As Long)
    If OldProc <> 0 Then Exit Sub
    mhWnd = hWnd
    OldProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WinProc)
End Sub

Public Sub UnHook()
    If OldProc = 0 Then Exit Sub
    SetWindowLong mhWnd, GWL_WNDPROC, OldProc
    OldProc = 0
End Sub
